<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  // Date handling : Start
  $msg ="";
  $party               = NULL;
  $partySelect         = NULL;
  $deliveryChalanArray = array();
  $deliveryChalanCount = 0;
  $GoodsReturnNote     = 0;
  $partyCondition      = NULL;
  $supplierCondition   = NULL;
	$select 						 = NULL;
	$where  						 = NULL;
	$join   						 = NULL;
	$union  						 = NULL;
  if(isset($_REQUEST['toDateYear']))
  {
	  $toDate    = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay']." 23:59:59";
  }
  else
  {
	 // $toDate    = date("Y-m-d");
	}
	if(isset($_REQUEST['fromDateYear']))
  {
	  $fromDate    = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay']." 00:00:00";
  }
  else
  {
	  $fromDate    = date("Y-m-d");
	}
	
	// Date handling : Start
	// party & supplior selected.
	
	if($_REQUEST['Challan'] == 'All')
  {
    $selectDeliveryChalan = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                                    deliverychallanmaster.srNo,user.userId,user.userName,DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
                                    supplier.supplierId,supplier.companyName
				  						         FROM deliverychallanmaster
					 						         JOIN user ON user.userId = deliverychallanmaster.userId
						 					         JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId
						 					        WHERE deliverychallanmaster.delivaryDate >= '".$fromDate."'
                                AND deliverychallanmaster.delivaryDate <= '".$toDate."'
                                AND deliverychallanmaster.status = 'A'
						 					        UNION 
						 					       SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                                    deliverychallanmaster.srNo,user.userId,user.userName,DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
                                    party.partyId,party.companyName
				  						         FROM deliverychallanmaster
					 						         JOIN user ON user.userId = deliverychallanmaster.userId
						 					         JOIN party ON party.partyId = deliverychallanmaster.partyId
						 					        WHERE deliverychallanmaster.delivaryDate >= '".$fromDate."'
                                AND deliverychallanmaster.delivaryDate <= '".$toDate."'
                                AND deliverychallanmaster.status = 'A'";
  }
  //condition when Goods Return Note
  elseif($_REQUEST['Challan'] == 'Goods Return Note')
  {
	  $GoodsReturnNote = 1;
   //condition when companyId selected in Goods Return Note 
   if(isset($_REQUEST['companyId']) && $_REQUEST['companyId'] != '')
	 {
	   $partyId = substr_replace($_REQUEST['companyId'],"",-1);
	   if(substr($_REQUEST['companyId'],-1) == 'P')
	   {
	   	$select = " ,party.partyId,party.companyName";
	   	$where  = " AND deliverychallanmaster.partyId = ".$partyId."";
	   	$join   = " JOIN party ON party.partyId    = deliverychallanmaster.partyId";
	   	$union = " UNION
	               SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                        deliverychallanmaster.srNo,deliverychallanmaster.status,user.userId,user.userName,
                        DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,supplier.supplierId,supplier.companyName
					         FROM deliverychallanmaster
					         JOIN user ON user.userId      = deliverychallanmaster.userId
					         JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId
					         JOIN deliverychallandetail ON deliverychallandetail.deliveryChallanMasterId = deliverychallanmaster.deliveryChallanMasterId
                   JOIN goodsreturn ON goodsreturn.deliveryChallanDetailId = deliverychallandetail.deliveryChallanDetailId
					        WHERE deliverychallanmaster.delivaryDate >= '".$fromDate."'
                    AND deliverychallanmaster.delivaryDate <= '".$toDate."'
                    AND deliverychallanmaster.status = 'A'";
	   }
	   elseif(substr($_REQUEST['companyId'],-1) == 'S')
	   {
	   	$select = " ,supplier.supplierId,supplier.companyName";
	   	$where  = " AND deliverychallanmaster.supplierId = ".$partyId."";
	   	$join   = " JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId";
	   	$union = " UNION
	               SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                        deliverychallanmaster.srNo,deliverychallanmaster.status,user.userId,user.userName,
                        DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,party.partyId,party.companyName
					         FROM deliverychallanmaster
					         JOIN user ON user.userId      = deliverychallanmaster.userId
					         JOIN party ON party.partyId    = deliverychallanmaster.partyId
					         JOIN deliverychallandetail ON deliverychallandetail.deliveryChallanMasterId = deliverychallanmaster.deliveryChallanMasterId
                   JOIN goodsreturn ON goodsreturn.deliveryChallanDetailId = deliverychallandetail.deliveryChallanDetailId
					        WHERE deliverychallanmaster.delivaryDate >= '".$fromDate."'
                    AND deliverychallanmaster.delivaryDate <= '".$toDate."'
                    AND deliverychallanmaster.status = 'A'";
	   }
	 }
	 else
	 {
	     $select = " ,party.partyId,party.companyName";
	     $join   = " JOIN party ON party.partyId    = deliverychallanmaster.partyId";
	     $union = " UNION
	               SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                        deliverychallanmaster.srNo,deliverychallanmaster.status,user.userId,user.userName,
                        DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,supplier.supplierId,supplier.companyName
					         FROM deliverychallanmaster
					         JOIN user ON user.userId      = deliverychallanmaster.userId
					         JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId
					         JOIN deliverychallandetail ON deliverychallandetail.deliveryChallanMasterId = deliverychallanmaster.deliveryChallanMasterId
                   JOIN goodsreturn ON goodsreturn.deliveryChallanDetailId = deliverychallandetail.deliveryChallanDetailId
					        WHERE deliverychallanmaster.delivaryDate >= '".$fromDate."'
                    AND deliverychallanmaster.delivaryDate <= '".$toDate."'
                    AND deliverychallanmaster.status = 'A'";
	 } 
   //condition when Goods Return Note
	 $selectDeliveryChalan = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                                   deliverychallanmaster.srNo,deliverychallanmaster.status,user.userId,user.userName,
                                   DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate
                                   ".$select."
  			  						        FROM deliverychallanmaster
  				 						        JOIN user ON user.userId      = deliverychallanmaster.userId
  				 						        JOIN deliverychallandetail ON deliverychallandetail.deliveryChallanMasterId = deliverychallanmaster.deliveryChallanMasterId
                              JOIN goodsreturn ON goodsreturn.deliveryChallanDetailId = deliverychallandetail.deliveryChallanDetailId
  				 						             ".$join."
  										       WHERE deliverychallanmaster.delivaryDate >= '".$fromDate."'
                               AND deliverychallanmaster.delivaryDate <= '".$toDate."'
                               AND deliverychallanmaster.status = 'A' 
                                   ".$where."
                                   ".$union."";
  }
  //Over condition when Goods Return Note
  else
  {
    if($_REQUEST['Challan'] == 'Delivery Challan/Note')
    {
	    $party = "JOIN party ON party.partyId = deliverychallanmaster.partyId";
	    $partySelect = "party.partyId,party.companyName";
    }
    if($_REQUEST['Challan'] == 'Goods Inward Note' || $_REQUEST['Challan'] == 'Goods Rejection')
    {
	    $party = "JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId";
	    $partySelect = "supplier.supplierId,supplier.companyName";
	  }
	  //condition when Goods Return Note is not selected
	  if($_REQUEST['Challan'] != 'Goods Return Note')
	  {
		  $selectDeliveryChalan = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
		                                    deliverychallanmaster.srNo,user.userId,user.userName,DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
		                                    ".$partySelect."
						  						         FROM deliverychallanmaster
							 				  		       JOIN user ON user.userId = deliverychallanmaster.userId
								 				                ".$party."";
		  //HERE Apply Logic of Party filtering
		  if(isset($_REQUEST['companyId']) && $_REQUEST['companyId'] != '')
	    {
	      	if($_REQUEST['Challan'] == 'Delivery Challan/Note')
	      	{
	          $selectDeliveryChalan .= " AND deliverychallanmaster.partyId = '".$_REQUEST['companyId']."' ";
	        }
	        else
	        {
	          $selectDeliveryChalan .= " AND deliverychallanmaster.supplierId = '".$_REQUEST['companyId']."' ";
	        }
	    }
	    if(isset($_REQUEST['Challan']) && $_REQUEST['Challan'] != '')
	    {
	      if($_REQUEST['Challan'] != '0' && $_REQUEST['Challan'] != 'Goods Return Note')
		    {
		      $selectDeliveryChalan .= " AND deliverychallanmaster.challanType = '".$_REQUEST['Challan']."' ";
		    }
	    }
	    if(isset($_REQUEST['toDateMonth'] ) && $_REQUEST['toDateMonth'] != '')
	    {
	      $selectDeliveryChalan .= " AND deliverychallanmaster.delivaryDate >= '".$fromDate."' 
	                                 AND deliverychallanmaster.delivaryDate <= '".$toDate."' 
	                               ORDER BY deliverychallanmaster.delivaryDate";
	    }
   }
  }
  $selectDeliveryResult = mysql_query($selectDeliveryChalan);
	while($deliveryChalanRow = mysql_fetch_array($selectDeliveryResult))
	{
	  $deliveryChalanArray[$deliveryChalanCount]['deliveryChallanMasterId'] = $deliveryChalanRow['deliveryChallanMasterId'];
	  
	  $deliveryChalanArray[$deliveryChalanCount]['companyName']             = $deliveryChalanRow['companyName'];
	  $deliveryChalanArray[$deliveryChalanCount]['srNo']                    = $deliveryChalanRow['srNo'];
	  $deliveryChalanArray[$deliveryChalanCount]['delivaryDate']            = $deliveryChalanRow['delivaryDate'];
	  $deliveryChalanArray[$deliveryChalanCount]['challanType']             = $deliveryChalanRow['challanType'];
	  $deliveryChalanCount++;
	}  
  if($deliveryChalanCount == 0)
  {
	  $msg = 'Record Not Found...!';
  }
  include("./bottom.php");
  $smarty->assign("msg",$msg);
  $smarty->assign("GoodsReturnNote",$GoodsReturnNote);
  $smarty->assign("deliveryChalanArray",$deliveryChalanArray);
  $smarty->display("deliveryChalanAj1.tpl");
}
?>